package com.activemq.springtest.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.listener.SessionAwareMessageListener;

import javax.annotation.Resource;
import javax.jms.*;

/**
 * 消费监听 bean1.xml
 */
public class TestSessionAwareMessageListenter implements SessionAwareMessageListener {


    @Autowired
    //@Resource(name = "respQueue")//Qualifier一样效果
    @Qualifier("respQueue")
    private Destination respQueue;

    //public void setRespQueue(Destination respQueue) {
    //    this.respQueue = respQueue;
    //}

    @Override
    public void onMessage(Message message, Session session) throws JMSException {
        if(message instanceof TextMessage){
            try {
                String text=((TextMessage)message).getText();
                System.out.println("spring中接收消息监听器接收到"+text);
                //创建
                MessageProducer producer = session.createProducer(respQueue);
                Message textMessage=session.createTextMessage("你好 收到");
                producer.send(textMessage);
                producer.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}
